

# 100票 卖票
import threading
import time

count = 100

class Seller(threading.Thread):
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name = name

    def run(self):
        # 声明全局变量
        global count
        global thredLock

        self.number = 100
        while count>0:
            # 加锁
            # 第一个运行到这里的线程加上锁 继续往下运行
            # 后面运行到这里的线程，必须等第一个线程解锁之后，抢锁
            thredLock.acquire()
            print(f'售票员{self.name},售出第{self.number-count+1}张票')
            count -= 1
            # 解锁
            thredLock.release()
            time.sleep(0.1)
        print(f'{self.name}说：”票全部卖完了“')

# 创建一把锁
thredLock = threading.Lock()

seller1 = Seller('西施')
seller2 = Seller('王昭君')

seller1.start()
seller2.start()

# seller1.join()
# seller2.join()

